package com.runjian.rundo.iot.agent.iec104.parser.frame.control.process;

import com.runjian.rundo.iot.agent.iec104.consts.Ti;
import com.runjian.rundo.iot.agent.iec104.parser.frame.InfoElement;
import com.runjian.rundo.iot.agent.iec104.parser.frame.SinglePointRemoteControl;
import com.runjian.rundo.iot.agent.iec104.parser.frame.control.base.YkControl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ByteUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * C_SC_NA_1(45, "(遥控)单命令")
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public class C_SC_NA_1 extends YkControl {

    @Override
    public void analysisInfo(byte[] msg) {
        this.addInfoElement(InfoElement.builder().address(ByteUtil.bytesToInt(new byte[] {msg[12], msg[13], msg[14], 0x00}))
            .value(new SinglePointRemoteControl(msg[15])).build());
    }

    @Override
    public void init() {
        super.init();
        // 类型
        this.setTi(Ti.C_SC_NA_1);
    }

    /**
     * 添加控制信息元素
     *
     * @param address 地址
     * @param value 数据
     */
    @Override
    public void addControlInfoElement(int address, Object value) {
        if (!(value instanceof Integer) || !(Integer.valueOf(value.toString()) >= 0 && Integer.valueOf(value.toString()) <= 1)) {
            Assert.isTrue(Boolean.FALSE, "值必须是整型数字,并且必须是（0,1）");
        }
        SinglePointRemoteControl singlePointRemoteControl = new SinglePointRemoteControl(true, Integer.valueOf(value.toString()));
        this.addInfoElement(InfoElement.builder().address(address).value(singlePointRemoteControl).build());
    }

}
